/*
 * File :
 * Created : 14 janv. 07
 * By : eSpider Team
 * Version : $Revision$
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details ( see the LICENSE file ).
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

package org.fbmc;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.util.Properties;

import org.fbmc.file.FileSniffer;
import org.fbmc.server.EmbeddedJettyServer;
import org.fbmc.server.EmbeddedSQLServer;
import org.fbmc.server.EmbeddedVLCServer;

public class FreeboxMediaCenter extends Properties {
	private static FreeboxMediaCenter instance;

	private final String[] propertiesToCheck = {"vlc.cmd", "vlc.play.picture", "sql.driver.url"};
	
	private EmbeddedJettyServer jettyServer;
	private EmbeddedVLCServer vlcServer;
	private EmbeddedSQLServer sqlServer;

	/**
	 * You can pass in parameter the configuration file to load. 
	 * Otherwise, the default file is loaded.
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		if (args.length > 0)
			instance = new FreeboxMediaCenter(args[0]);
		else
			instance = new FreeboxMediaCenter("./etc/fbmc.properties");
		
		instance.start();
		
		new Thread(){
			@Override
			public void run() {
				try{
					String line = new BufferedReader(new InputStreamReader(System.in)).readLine();
					System.out.println("line : " + line);
					System.exit(1);
				}catch(Exception e){
					e.printStackTrace();
				}
			}
		}.start();
	}

	/**
	 * Singleton
	 * 
	 * @return
	 */
	public static FreeboxMediaCenter getInstance() {
		return instance;
	}

	/**
	 * Constructor
	 * 
	 * @param propertiesFile configuration file (properties format)
	 */
	public FreeboxMediaCenter(String propertiesFile) {
		try {
			load(propertiesFile);
						
			Runtime.getRuntime().addShutdownHook(new Thread(){				
				public void run() {
					jettyServer.shutdown();
					vlcServer.shutdown();
					sqlServer.shutdown();
				}
			});
						
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 
	 *
	 */
	public void start() {
		try{
			jettyServer = new EmbeddedJettyServer();
			vlcServer = new EmbeddedVLCServer();
			sqlServer = new EmbeddedSQLServer();
			
			
			new FileSniffer().start();			
		}catch (Exception e) {
			e.printStackTrace();
			System.exit(0);
		}
	}

	/**
	 * Load the configuration file and check if all the properties are defined
	 * 
	 * @param file path of configuration file
	 * @throws Exception
	 */
	private void load(String file) throws Exception {
		load(new FileReader(file));
		
		for (String propertyToCheck : propertiesToCheck) {
			if (getProperty(propertyToCheck) == null)
				throw new Exception("The property '" + propertyToCheck + "' hasn't found...");
		}
	}
	
	
	public EmbeddedVLCServer getVLCServer(){
		return vlcServer;
	}
	
	public EmbeddedSQLServer getSQLServer(){
		return sqlServer;
	}
	
	public EmbeddedJettyServer getJettyServer(){
		return jettyServer;
	}
}
